package com.example.javaeefinal.controller;

import com.example.javaeefinal.Service.Imp.RoleServiceImpl;
import com.example.javaeefinal.Service.RoleService;
import com.example.javaeefinal.pojo.Role;
import com.example.javaeefinal.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@CrossOrigin
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequestMapping("/getById")
    @ResponseBody
    public Result<Role> getById(Integer id){
        Role role=roleService.getById(id);
        if(role != null){
            String code="0";
            String msg="根据id查询成功";
            return Result.success(code,role,msg);
        }
        else{
            String code="1";
            String msg="根据id查询失败";
            return Result.error(code,msg);
        }
    }
    @RequestMapping("/getByName")
    @ResponseBody
    public Result<Role> getByName(String name){
        Role role=roleService.getByName(name);
        if(role != null){
            String code="0";
            String msg="根据名字查询成功";
            return Result.success(code,role,msg);
        }
        else{
            String code="1";
            String msg="根据名字查询失败";
            return Result.error(code,msg);
        }
    }
    @RequestMapping("/getAll")
    @ResponseBody
    public Result<List<Role>> getAll(){
        List<Role> roleList= roleService.getAll();
        if(roleList != null){
            String code="0";
            String msg="返回全部角色成功";
            return Result.success(code,roleList,msg);
        }
        else{
            String code="1";
            String msg="返回全部角色失败";
            return Result.error(code,msg);
        }
    }
    @RequestMapping("/add")
    @ResponseBody
    public Result<Role> add(String name){
        boolean flag=roleService.add(name);
        if(flag != false){
            String code="0";
            String msg="新增角色成功";
            return Result.success(code,msg);
        }
        else{
            String code="1";
            String msg="新增角色失败";
            return Result.error(code,msg);
        }
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Result<Role> delete(Integer id){
        boolean flag=roleService.delete(id);
        if(flag != false){
            String code="0";
            String msg="删除角色成功";
            return Result.success(code,msg);
        }
        else{
            String code="1";
            String msg="删除角色失败";
            return Result.error(code,msg);
        }
    }
    @RequestMapping("/updateById")
    @ResponseBody
    public Result<Role> updateById(Integer id,String name){
        boolean flag=roleService.updateById(id,name);
        if(flag != false){
            String code="0";
            String msg="更新角色名字成功";
            return Result.success(code,msg);
        }
        else{
            String code="1";
            String msg="更新角色名字失败";
            return Result.error(code,msg);
        }
    }
}
